package cn.zjutleo.village_server.service;

import cn.zjutleo.village_server.dto.PolicyDto;
import cn.zjutleo.village_server.dto.PolicySelectDto;
import cn.zjutleo.village_server.exception.apiException.daoException.SelectException;
import cn.zjutleo.village_server.mapper.PolicyMapper;
import cn.zjutleo.village_server.vo.PolicyVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PolicyService {
    @Resource
    private PolicyMapper policyMapper;

    //根据ID查找政策，找不到返回报错
    public PolicyVo getExistPolicyVoById(Integer policy_id) throws SelectException {
        PolicyVo policyVo = policyMapper.selectByPrimaryKey(policy_id);
        if (policyVo == null) {
            try {
                throw new SelectException("政策不存在！");
            } catch (SelectException e) {
                e.printStackTrace();
            }
        }
        return policyVo;
    }


    //根据id查找政策详细信息，查不到报错
    public PolicyVo getExistPolicyVoDetailById(Integer policy_id) throws SelectException {
        PolicyVo policyvo = policyMapper.selectByPrimaryKey(policy_id);
        if (policyvo == null) {
            throw new SelectException("政策不存在！");
        }
        return policyvo;
    }


    //保存
    public void savePolicy(PolicyVo policyvo) {
        policyMapper.insertSelective(policyvo);
    }

    //删除
    public void deletePolicyById(Integer id) {
        policyMapper.deleteByPrimaryKey(id);
    }

    //更新
    public void updatePolicyById(PolicyDto policyDto) {
        PolicyVo policyvo = new PolicyVo();
        BeanUtils.copyProperties(policyDto, policyvo);
        policyMapper.updateByPrimaryKeySelective(policyvo);
    }

    public List<PolicyDto> selectPolicyList(PolicySelectDto policySelectDto) {
        List<PolicyDto> result = policyMapper.selectPolicyList(policySelectDto);
        return result;
    }

    public PageInfo<PolicyDto> selectPolicyPage(PolicySelectDto policySelectDto,Integer start,Integer limit) {
        PageHelper.startPage(start, limit);
        Page<PolicyDto> tmp = policyMapper.selectPolicyPage(policySelectDto);
        PageInfo<PolicyDto> result=new PageInfo<>(tmp);
        return result;
    }
}

